package com.rizzo.analytics.services.init;

import com.google.gag.annotation.literary.verse.BurmaShave;
import com.rizzo.analytics.domain.account.Account;
import com.rizzo.analytics.domain.trigger.TresholdType;
import com.rizzo.analytics.domain.trigger.Trigger;
import com.rizzo.analytics.services.repo.AccountRepository;
import com.rizzo.analytics.services.repo.TriggerRepository;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Date;

public class LocalDBInitializer implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void cleanDatabase() {
        TriggerRepository triggerRepository = (TriggerRepository) this.applicationContext.getBean("triggerRepository");
        triggerRepository.deleteAll();
        AccountRepository accountRepository = (AccountRepository) this.applicationContext.getBean("accountRepository");
        accountRepository.deleteAll();
    }

    public void createStandardAccounts() {
        AccountRepository accountRepository = (AccountRepository) this.applicationContext.getBean("accountRepository");
        if (accountRepository.count() == 0) {

            Account jalie = createAccount("Jan", "Lievens", "jalie", "Y5XOEUn6", "jan.lievens@gmail.com",
                    Account.ROLE_ADMIN + "," + Account.ROLE_USER, false, false, false, new Date(), new Date());

            Trigger triggerDeviation = createTrigger(jalie, "0", "dev",
                    "com.rizzo.analytics.services.web.controller.v1.AnalyticsController.getInfo", 0.5D, TresholdType.DEVIATION,
                    1L, new Date());

            Trigger triggerQuota = createTrigger(jalie, null, null,
                    "com.rizzo.analytics.services.web.controller.v1.AnalyticsController.getInfo", 300D, TresholdType.QUOTA,
                    1L, new Date());

            Trigger triggerMean = createTrigger(jalie, null, null,
                    "com.rizzo.analytics.services.web.controller.v1.AnalyticsController.getInfo", 3D, TresholdType.MEAN,
                    1L, new Date());

            jalie.getTriggers().add(triggerDeviation);
            jalie.getTriggers().add(triggerQuota);
            jalie.getTriggers().add(triggerMean);

            accountRepository.save(jalie);

            Account babeth = createAccount("Elisabeth", "Hofman", "babeth", "babeth", "hofman.elisabeth@gmail.com",
                    Account.ROLE_USER, false, false, false, new Date(), new Date());

            Trigger triggerMin = createTrigger(babeth, null, null,
                    "com.rizzo.analytics.services.web.controller.v1.AnalyticsController.getInfo", 20D, TresholdType.HITS,
                    1L, new Date());

            babeth.getTriggers().add(triggerMin);

            accountRepository.save(babeth);
        }

    }

    public void createTestData() {
        AccountRepository accountRepository = (AccountRepository) this.applicationContext.getBean("accountRepository");
        Account jalie = createAccount("Jan", "Lievens", "jalie", "Y5XOEUn6", "jan.lievens@gmail.com",
                Account.ROLE_ADMIN + "," + Account.ROLE_USER, false, false, false, new Date(), new Date());

        Trigger jalieDeviation = createTrigger(jalie, "0", "dev", "com.rizzo.analytics.services.web.controller.v1.InfoController.getInfo",
                0.5D, TresholdType.DEVIATION, 1L, new Date());

        Trigger jalieQuota = createTrigger(jalie, "1", "instance", "com.rizzo.analytics.services.web.controller.v1.AccountController.getTriggers",
                300D, TresholdType.QUOTA, null, new Date());

        Trigger jalieMean = createTrigger(jalie, null, null, "com.rizzo.analytics.services.web.controller.v1.ApplicationController.getApplications",
                3D, TresholdType.MEAN, null, new Date());

        Trigger jalieHits = createTrigger(jalie, null, null, "com.rizzo.analytics.services.web.controller.v1.TrendController.getTrend",
                20D, TresholdType.HITS, null, new Date());

        jalie.getTriggers().add(jalieDeviation);
        jalie.getTriggers().add(jalieQuota);
        jalie.getTriggers().add(jalieMean);
        jalie.getTriggers().add(jalieHits);

        accountRepository.save(jalie);

        Account babeth =
                createAccount("Elisabeth", "Hofman", "babeth", "babeth", "hofman.elisabeth@gmail.com", Account.ROLE_USER, false, false, false, new Date(), new Date());

        Trigger babethMin = createTrigger(babeth, null, null, "com.rizzo.analytics.services.web.controller.v1.PerformanceController.getPerformance",
                20D, TresholdType.MIN, null, new Date());

        babeth.getTriggers().add(babethMin);

        accountRepository.save(babeth);

        Account kapitein = createAccount("Kapitein", "Zeppos", "kapitein", "12345678", "someone@gmail.com", Account.ROLE_ADMIN, true, false, true, new Date(), new Date());

        @BurmaShave
        Trigger kapiteinHits = createTrigger(kapitein, null, null, "com.rizzo.analytics.services.web.controller.v1.TrendController.getTrend",
                20D, TresholdType.HITS, null, new Date());

        Trigger kapiteinQuota = createTrigger(kapitein, null, null, "com.rizzo.analytics.services.web.controller.v1.TrendController.getTrend",
                2000D, TresholdType.QUOTA, null, new Date());

        kapitein.getTriggers().add(kapiteinHits);
        kapitein.getTriggers().add(kapiteinQuota);

        accountRepository.save(kapitein);

        Account piet = createAccount("Piet", "Snot", "piet", "bluemonday", "someone-else@hotmail.com", Account.ROLE_USER, false, false, true, new Date(), new Date());

        accountRepository.save(piet);

    }

    private Trigger createTrigger(Account jalie, String instanceIndex, String instanceName, String service,
                                  double treshold, TresholdType tresholdType, Long version, Date created) {
        Trigger triggerDeviation = new Trigger();
        triggerDeviation.setInstanceIndex(instanceIndex);
        triggerDeviation.setInstanceName(instanceName);
        triggerDeviation.setService(service);
        triggerDeviation.setTreshold(treshold);
        triggerDeviation.setTresholdType(tresholdType);
        triggerDeviation.setVersion(version);
        triggerDeviation.setAccount(jalie);
        triggerDeviation.setCreated(created);
        return triggerDeviation;
    }

    private Account createAccount(String firstname, String lastname, String username, String password, String email,
                                  String roles, boolean locked, boolean disabled, boolean expired, Date lastLogin, Date lastUpdated) {
        Account account = new Account();
        account.setFirstname(firstname);
        account.setLastname(lastname);
        account.setUsername(username);
        account.setPassword(password);
        account.setEmail(email);
        account.setRoles(roles);
        account.setLocked(locked);
        account.setDisabled(disabled);
        account.setExpired(expired);
        account.setLastLogin(lastLogin);
        account.setLastUpdated(lastUpdated);
        return account;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
